﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class AllocateRuleBLL : IBaseInfo
    {
        private static readonly IAllocateRuleDA allocateRuleDA = DataAccess.CreateAllocateRuleDA();
        private static readonly IAllocateRuleDetailDA allocateRuleDetailDA = DataAccess.CreateAllocateRuleDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public AllocateRuleBLL()
        {
            this.pubParasInfo = null;
        }

        public AllocateRuleBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public AllocateRuleBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> allocateRuleIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<AllocateRuleInfo> allocateRuleInfos = new List<AllocateRuleInfo>();
                AllocateRuleInfo allocateRuleInfo = null;
                foreach (string id in allocateRuleIDs)
                {
                    allocateRuleInfo = allocateRuleDA.GetAllocateRuleByID(dataBase, tran, id, this.dataBaseID[0]);
                    allocateRuleInfos.Add(allocateRuleInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                allocateRuleDetailDA.DeleteAllocateRuleDetail(dataBase, tran, allocateRuleIDs, this.dataBaseID);
                if (allocateRuleDA.DeleteAllocateRule(dataBase, tran, allocateRuleIDs, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < allocateRuleIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<AllocateRuleInfo>(dataBase, tran, allocateRuleInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ALLOCATE_RULE", this.dataBaseID[0]);
                        temp = temp + allocateRuleIDs[i] + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return allocateRuleDA.GetAllAllocateRule(this.dataBaseID[0]);
        }

        public object GetInfoByID(string allocateRuleID)
        {
            AllocateRuleInfo allocateRule = allocateRuleDA.GetAllocateRuleByID(allocateRuleID, this.dataBaseID[0]);
            if (allocateRule != null)
            {
                allocateRule.AllocateRuleDetailInfo = allocateRuleDetailDA.GetAllocateRuleDetailListByID(allocateRuleID, this.dataBaseID[0], this.pubParasInfo);
            }
            return allocateRule;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            AllocateRuleQueryEntity allocateRuleQuery = obj as AllocateRuleQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(allocateRuleQuery.AllocateRuleID))
            {
                sqlWhere.Add(" ALLOCATE_RULE_ID " + allocateRuleQuery.AllocateRuleID);
            }
            if (!string.IsNullOrEmpty(allocateRuleQuery.Descr))
            {
                sqlWhere.Add(" Descr " + allocateRuleQuery.Descr);
            }
            if (!string.IsNullOrEmpty(allocateRuleQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + allocateRuleQuery.IsActive);
            }
            return allocateRuleDA.GetAllocateRuleByQueryList(sqlWhere, allocateRuleQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string allocateRuleID = string.Empty;
            try
            {
                AllocateRuleInfo allocateRule = obj as AllocateRuleInfo;
                if (allocateRuleDA.CheckAllocateRuleIDUnique(dataBase, tran, allocateRule.AllocateRuleID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2440_001", this.pubParasInfo.UserLanguage, new List<string> { allocateRule.AllocateRuleID });
                }
                allocateRule.UpdatedBy = allocateRule.CreatedBy;
                allocateRule.CreatedDate = DateTime.Now;
                allocateRule.UpdatedDate = DateTime.Now;
                allocateRuleDA.InsertAllocateRule(allocateRule, this.dataBaseID);
                allocateRuleID = allocateRule.AllocateRuleID;
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<AllocateRuleInfo>(dataBase, tran, null, allocateRule, AuditAction.Insert, this.pubParasInfo.UserID, "ALLOCATE_RULE", this.dataBaseID[0]);
                if ((allocateRule.AllocateRuleDetailInfo != null) && (allocateRule.AllocateRuleDetailInfo.Count > 0))
                {
                    for (int i = 0; i < allocateRule.AllocateRuleDetailInfo.Count; i++)
                    {
                        if (allocateRuleDetailDA.CheckAllocateRuleDetailIDUnique(dataBase, tran, allocateRule.AllocateRuleDetailInfo[i].AllocateRuleID, allocateRule.AllocateRuleDetailInfo[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F2440_002", this.pubParasInfo.UserLanguage, new List<string> { allocateRule.AllocateRuleDetailInfo[i].AllocateRuleID, allocateRule.AllocateRuleDetailInfo[i].LineID.ToString() });
                        }
                        else
                        {
                            allocateRule.AllocateRuleDetailInfo[i].CreatedBy = allocateRule.CreatedBy;
                            allocateRule.AllocateRuleDetailInfo[i].UpdatedBy = allocateRule.UpdatedBy;
                            allocateRule.AllocateRuleDetailInfo[i].CreatedDate = DateTime.Now;
                            allocateRule.AllocateRuleDetailInfo[i].UpdatedDate = DateTime.Now;
                            allocateRuleDetailDA.InsertAllocateRuleDetail(dataBase, tran, allocateRule.AllocateRuleDetailInfo[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<AllocateRuleDetailInfo>(dataBase, tran, null, allocateRule.AllocateRuleDetailInfo[i], AuditAction.Insert, this.pubParasInfo.UserID, "ALLOCATE_RULE_DETAIL", this.dataBaseID[0]);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return allocateRuleID;
        }

        public string Update(object obj)
        {
            string allocateRuleID = string.Empty;
            try
            {
                AllocateRuleInfo allocateRule = obj as AllocateRuleInfo;
                AllocateRuleInfo oldAllocateRuleInfo = allocateRuleDA.GetAllocateRuleByID(allocateRule.AllocateRuleID, this.dataBaseID[0]);
                if (allocateRuleDA.UpdateAllocateRule(allocateRule, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<AllocateRuleInfo>(oldAllocateRuleInfo, allocateRule, AuditAction.Update, this.pubParasInfo.UserID, "ALLOCATE_RULE", this.dataBaseID[0]);
                    allocateRuleID = allocateRule.AllocateRuleID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return allocateRuleID;
        }
    }
}

